#define BLINKER_WIFI
#include <Blinker.h>

char auth[] = "2d3647036ac2";
char ssid[] = "Hello";
char pswd[] = "123456789";

BlinkerNumber HUMI("Humidity");
BlinkerNumber TEMP("Temperature");
BlinkerButton ButtonBtu1("btn-1");
BlinkerButton ButtonBtu2("btn-2");
BlinkerSlider SliderTemp("SliderTemp");    
BlinkerSlider SliderHumi("SliderHumi");   
#include <DHT.h>

#define DHTPIN 8
#define DHTTYPE DHT11   // DHT 11

#define HUMI_CONTROL_PIN 10
#define TMEP_CONTROL_PIN 11
    
DHT dht(DHTPIN, DHTTYPE);

uint32_t read_time = 0;
float humi_read, temp_read;

void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
    
    uint32_t BlinkerTime = millis();
    
    Blinker.print("millis", BlinkerTime);
}

void heartbeat(void)
{
    HUMI.print(humi_read);
    TEMP.print(temp_read);
}

void dataStorage()
{
    Blinker.dataStorage("Temperature", temp_read);
    Blinker.dataStorage("Humidity", humi_read);
}

void ButtonHumi_callback(const String & state){

   if (state == BLINKER_CMD_ON ){
        ButtonBtu1.print("on");
    }
    else if(state == BLINKER_CMD_OFF){
        ButtonBtu1.print("off");
    }
}

void ButtonTemp_callback(const String & state){

   if (state == BLINKER_CMD_ON ){
        ButtonBtu2.print("on");
    }
    else if(state == BLINKER_CMD_OFF){
     
        ButtonBtu2.print("off");
    }
}

void SliderTemp_callback(int32_t value)
{
  BLINKER_LOG("get slider value: ", value);
}

void SliderHumi_callback(int32_t value)
{
  BLINKER_LOG("get slider value: ", value);
}

void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    Blinker.attachHeartbeat(heartbeat);
    ButtonBtu1.attach(ButtonHumi_callback);
    ButtonBtu2.attach(ButtonTemp_callback);
    SliderTemp.attach(SliderTemp_callback);
    SliderHumi.attach(SliderHumi_callback); 
    //dht.begin();
}

void loop()
{
    Blinker.run();

    if (read_time == 0 || (millis() - read_time) >= 2000)
    {
        read_time = millis();

        float h = 0;//dht.readHumidity();
        float t = 0;//dht.readTemperature();        

        if (isnan(h) || isnan(t)) {
            BLINKER_LOG("Failed to read from DHT sensor!");
            return;
        }

  float hic = dht.computeHeatIndex(t, h, false);
  
     humi_read = h;
     temp_read = t;
    }
}
